package com.zh.work.service.platform.channel.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.work.dao.platform.channel.PayRouteMapper;
import com.zh.work.entity.channel.PayRouteEntity;
import com.zh.work.entity.channel.QrCodeRouteMerchEntity;
import com.zh.work.service.platform.channel.PayRouteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 代付路由规则设置业务逻辑实现层
 * @Author: Li.HuiNeng
 * @Create in: 2019/12/26
 * @Version: 1.0.1
 */
@Service
public class PayRouteServiceImpl extends ServiceImpl<PayRouteMapper, PayRouteEntity> implements PayRouteService {

    private static final Logger log = LoggerFactory.getLogger(PayRouteServiceImpl.class);

    @Autowired
    private PayRouteMapper mapper;


    @Override
    public IPage<PayRouteEntity> getPagePayRoutes(Page<PayRouteEntity> page, PayRouteEntity param) {
        return mapper.getPagesPayRoutes(page, param.getBranchNo(), param.getStatus(), param.getRouteName(), param.getMerchNo());
    }

    @Override
    public int putAddPayRoute(PayRouteEntity payRouteEntity) throws Exception {
        // 新增代付路由规则
        if (mapper.insert(payRouteEntity) <= 0) return -1;
        // 解析代付路由商户列表
        List<QrCodeRouteMerchEntity> list = JSON.parseArray(payRouteEntity.getMerchantRouteJson(), QrCodeRouteMerchEntity.class);
        if (list != null) {
            if (list.size() > 0) {
                list.forEach(f -> f.setRouteId(payRouteEntity.getId()));
                // 新增代付路由商户
                if (mapper.addPayRouteMerchant(list) <= 0) return -2;
            } else
                log.info("代付路由商户为空!" );
        }
        return 1;
    }

    @Override
    public int putModPayRouteById(PayRouteEntity payRouteEntity) throws Exception {
        // 查询已存在代付路由商户数量
        Map<String, Long> map = mapper.getPayMerchantNum(payRouteEntity.getId());
        if (map.get("num" ) > 0) {
            if (mapper.delPayMerchantById(payRouteEntity.getId()) <= 0) return -1;
        }
        // 解析代付路由商户列表
        List<QrCodeRouteMerchEntity> list = JSON.parseArray(payRouteEntity.getMerchantRouteJson(), QrCodeRouteMerchEntity.class);
        if (list != null) {
            if (list.size() > 0) {
                list.forEach(f -> f.setRouteId(payRouteEntity.getId()));
                // 新增代付路由商户
                if (mapper.addPayRouteMerchant(list) <= 0) return -2;
            } else log.info("代付路由商户为空!" );
        }
        // 修改代付路由信息
        if (mapper.updateById(payRouteEntity) <= 0) return -3;
        return 1;
    }
}
